#! /usr/bin/perl -w
use Getopt::Long;
use Pod::Usage;

# Options
my $clean = ''; 
my $help = '';
my $man = '';

GetOptions ('clean' => \$clean, 
            'help|?' => \$help ,
            'man' => \$man
    )
or pod2usage(2);

pod2usage(1) if ( $help );
pod2usage(-verbose => 2) if $man;

if ( $clean )
{
    print "Cleaning all files\n";
    system( "make clean > /dev/null");
    system( "rm -rf gcov_prof");
    system( "rm -rf comp.log run_test.log run_gcov.log run_gcov.err");
    exit 0;
}

system( "make utest 1>comp.log 2>&1") == 0 
    or die "Compilation error. Check comp.log\n";
system( "rm -rf *.gcda ");
system( "make run_test 1>run_test.log 2>&1");
if ( $? != 0)
{
    print "Run ./utest failed. Check run_test.log\n";
}

system( "gcov -a -b -d -f test.cpp 1>run_gcov.log 2>run_gcov.err");
if ( $? != 0)
{
    print "Run gcov failed. Check run_gcov.err\n";
} else
{
    print "Check gcov logs: run_gcov.log\n";
    print "Check detailed gcov logs in 'gcov_prof' directory\n";
}
system( "rm -rf gcov_prof");
system( "mkdir gcov_prof");
system( "mv *.gcov gcov_prof");

__END__

=head1 NAME

run_all.pl - Running unit tests

=head1 SYNOPSIS

./run_all.pl [options]

 Options:
   -help            brief help message
   -clean           clean working directory

=head1 OPTIONS

=over 8

=item B<-help>

Print a brief help message and exits.

=item B<-clean>

Clean working directory and exits.

=back

=head1 DESCRIPTION

B<This program> will compile utest, run it and make profiling by gcov.

=cut
